package com.scujj.cs6.controller;

import com.scujj.cs6.annotation.AuthCheck;
import com.scujj.cs6.common.Result;
import com.scujj.cs6.model.vo.UserFavoriteVo;
import com.scujj.cs6.service.UserFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户收藏接口", description = "用户收藏相关 API")
@RestController
@RequestMapping("/favorites")
@Validated
public class UserFavoriteController {
    @Autowired
    private UserFavoriteService userFavoriteService;

    @Operation(summary = "收藏回答", description = "用户登录后可收藏回答")
    @PostMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> favoriteAnswer(@Parameter(name = "id" , description = "回答ID", required = true , in = ParameterIn.PATH) @PathVariable Long id, HttpServletRequest request) {
        userFavoriteService.favoriteAnswer(id,request);
        return Result.success();
    }

    @Operation(summary = "取消收藏", description = "用户登录后可取消收藏")
    @DeleteMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> cancelFavoriteAnswer(
            @Parameter(name = "id" , description = "回答ID", required = true , in = ParameterIn.PATH)
            @PathVariable Long id, HttpServletRequest request) {
        userFavoriteService.cancelFavorite(id, request);
        return Result.success();
    }


    @Operation(summary = "获取用户收藏列表", description = "用户登录后可获取收藏列表")
    @GetMapping("/")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(arraySchema = @Schema(implementation = UserFavoriteVo.class))
            )
    )
    public Result<List<UserFavoriteVo>> getFavorites(HttpServletRequest request) {
        return Result.success(userFavoriteService.getFavorites(request));
    }
}
